<template>
   <a-spin :spinning="confirmLoading">
     <j-form-container :disabled="formDisabled">
       <!-- 主表单区域 -->
       <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
         <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="户号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusId">
              <j-dict-select-tag type="list" v-model="model.cusId"  dictCode="mws_t_arc_customers,code,id" placeholder="请选择户号" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <a-input v-model="model.cusName" placeholder="请输入户名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="财务月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financialMonth">
              <a-input v-model="model.financialMonth" placeholder="请输入财务月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="预存缴费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storedPaymentMoney">
              <a-input-number v-model="model.storedPaymentMoney" placeholder="请输入预存缴费金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="是否代收" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isProxy">
              <j-dict-select-tag type="list" v-model="model.isProxy"  dictCode="yn" placeholder="请选择是否代收" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="缴费批号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchNo">
              <a-input v-model="model.batchNo" placeholder="请输入缴费批号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="代收的垃圾费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ljFee">
              <a-input-number v-model="model.ljFee" placeholder="请输入代收的垃圾费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status"  dictCode="" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="缴费类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feeTargetType">
              <j-dict-select-tag type="list" v-model="model.feeTargetType"  dictCode="fee_target_type" placeholder="请选择缴费类型" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
     </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="缴费明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="chgSubPaymentsTable.loading"
            :columns="chgSubPaymentsTable.columns"
            :dataSource="chgSubPaymentsTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
</template>

<script>

  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ChgPaymentsForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        validatorRules: {
           cusId: [
              { required: true, message: '请输入户号!'},
           ],
           financialMonth: [
              { required: true, message: '请输入财务月份!'},
           ],
           storedPaymentMoney: [
              { required: true, message: '请输入预存缴费金额!'},
           ],
           money: [
              { required: true, message: '请输入金额!'},
           ],
           isProxy: [
              { required: true, message: '请输入是否代收!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ],
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['chgSubPayments', ],
        tableKeys:['chgSubPayments', ],
        activeKey: 'chgSubPayments',
        // 缴费明细
        chgSubPaymentsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '付款方式',
              key: 'feePayType',
              type: FormTypes.select,
              dictCode:"fee_pay_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '金额',
              key: 'money',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/chg/chgPayments/add",
          edit: "/chg/chgPayments/edit",
          chgSubPayments: {
            list: '/chg/chgPayments/queryChgSubPaymentsByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
     addBefore(){
            this.chgSubPaymentsTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.chgSubPayments.list, params, this.chgSubPaymentsTable)
        }
      },
      //校验所有一对一子表表单
    validateSubForm(allValues){
        return new Promise((resolve,reject)=>{
          Promise.all([
          ]).then(() => {
            resolve(allValues)
          }).catch(e => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
        })
    },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          chgSubPaymentsList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     close() {
        this.visible = false
        this.$emit('close')
        this.$refs.form.clearValidate();
      },

    }
  }
</script>

<style scoped>
</style>